using System;
using System.Net;
using SharpSvn;

namespace BuildBox.Core.SourceControl
{
    public class SvnProvider : ISourceControlProvider
    {
        private readonly SvnClient _svnClient;
        private readonly SvnUriTarget _url;
        private readonly string _workingDirectory;

        public SvnProvider(string url, string workingDirectory, string userName, string password)
        {
            _url = new SvnUriTarget(url);
            _workingDirectory = workingDirectory;

            _svnClient = new SvnClient();
            if (!string.IsNullOrEmpty(userName))
            {
                _svnClient.Authentication.DefaultCredentials = new NetworkCredential(userName, password);
            }
        }

        public void Initialize()
        {
            _svnClient.CheckOut(_url, _workingDirectory);
        }

        public void GetLatest()
        {
            _svnClient.Update(_workingDirectory);
        }

        public void Revert()
        {
            _svnClient.Revert(_workingDirectory);
        }

        public void Dispose()
        {
            if (_svnClient != null)
            {
                _svnClient.Dispose();
            }
        }
    }
}